package io.jt808.proto.req;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 定位数据批量上传
 */
@Proto("0704")
@Data
public class JT0704 implements IProto {
    /**
     * 定位包数量
     */
    private short packetNum;
    /**
     * 汇报数据类型，0 正常批量位置汇报 1 盲区补报
     */
    private byte type;

    private List<Position> positions;

    public byte[] writeToBytes() {
        AutoBuffer buff = new AutoBuffer();
        buff.writeShort(packetNum);
        buff.writeByte(type);
        for (Position p : positions) {
            buff.writeShort(p.dataLength);
            buff.writeBytes(p.position.writeToBytes());
        }
        return buff.array();

    }

    public void readFromBuf(AutoBuffer buff) {
        packetNum = buff.readShort();
        type = buff.readByte();
        positions = new ArrayList<>();
        while (buff.isReadable()) {
            Position p = new Position();
            p.dataLength = buff.readShort();
            JT0200 jt0200 = new JT0200();
            jt0200.readFromBuf(buff.slice(buff.readerIndex(), p.dataLength));
            p.position = jt0200;
            buff.skipBytes(p.dataLength);
            positions.add(p);
        }

    }

    public class Position {
        private short dataLength;
        private JT0200 position;
    }

//    public static void main(String[] args) {
////        byte[] bytes=new byte[]{
////                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x01,0x59,0x5e,
////                0x32,0x06, (byte) 0xbc, (byte) 0xfd,0x58,0x00,0x00,0x02, (byte) 0x94,0x00,0x00,
////                0x18,0x10,0x24,0x14,0x19,0x36,0x01,0x04,0x00,0x01,0x1a, (byte) 0xbb
////        };
//        byte[] bytes = new byte[]{
//                0x00, 0x02,
//                0x00,
//                0x00, 0x22,
//                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x59, 0x5e,
//                0x32, 0x06, (byte) 0xbc, (byte) 0xfd, 0x58, 0x00, 0x00, 0x02, (byte) 0x94, 0x00, 0x00,
//                0x18, 0x10, 0x24, 0x14, 0x19, 0x36, 0x01, 0x04, 0x00, 0x01, 0x1a, (byte) 0xbb,
//                0x00, 0x22,
//                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x59, 0x5e,
//                0x32, 0x06, (byte) 0xbc, (byte) 0xfd, 0x58, 0x00, 0x00, 0x02, (byte) 0x94, 0x00, 0x00,
//                0x18, 0x10, 0x24, 0x14, 0x19, 0x36, 0x01, 0x04, 0x00, 0x01, 0x1a, (byte) 0xbb,
//        };
//        AutoBuffer buffer = new AutoBuffer(bytes);
//        JT0704 jt0200 = new JT0704();
//        jt0200.readFromBuf(buffer);
//        System.out.println(new String(bytes).equals(new String(jt0200.writeToBytes())));
////        System.out.println(Tools.ToHexString(bytes));
////        System.out.println(Tools.ToHexString(jt0200.writeToBytes()));
//
//    }
}